﻿using System;
using System.IO;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Web;
using System.Web.Security;
using System.Security.Cryptography;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;

using Jungle.Code.DAL;

namespace Jungle.Prodamo.Core
{
    public class UploadFiles
    {
        private string ConnectionString = ConfigurationManager.ConnectionStrings["dbConnectionUserBase"].ConnectionString;
        private int fileId;
        private int userId;
        private string title;
        private string fileLink;
        private bool enabled;
        private DateTime pubDate;
        private int count;

        public List<UploadFiles> FileList;

        public int FileId { get { return fileId; } set { fileId = value; } }
        public int UserId { get { return userId; } set { userId = value; } }
        public string Title { get { return title; } set { title = value; } }
        public string FileLink { get { return fileLink; } set { fileLink = value; } }
        public bool Enabled { get { return enabled; } set { enabled = value; } }
        public DateTime PubDate { get { return pubDate; } set { pubDate = value; } }
        public int Count { get { return count; } set { count = value; } }

        public void Add()
        {
            using (SqlConnection connection = new SqlConnection(ConnectionString))
            {
                SqlCommand command = new SqlCommand("UploadFiles_Add", connection);
                SqlParameter param_UserId = command.Parameters.Add("@UserId", SqlDbType.Int);
                SqlParameter param_Title = command.Parameters.Add("@Title", SqlDbType.NChar, 255);
                SqlParameter param_FileLink = command.Parameters.Add("@FileLink", SqlDbType.Text);
                SqlParameter param_Enabled = command.Parameters.Add("@Enabled", SqlDbType.Bit);

                param_UserId.Value = userId;
                param_Title.Value = title;
                param_FileLink.Value = fileLink;
                param_Enabled.Value = enabled;

                command.CommandType = CommandType.StoredProcedure;
                command.Connection.Open();
                command.ExecuteNonQuery();
                command.Connection.Close();
            }
        }
        public void Delete()
        {
            using (SqlConnection connection = new SqlConnection(ConnectionString))
            {
                SqlCommand command = new SqlCommand("UploadFiles_Delete", connection);
                SqlParameter param_FileId = command.Parameters.Add("@FileId", SqlDbType.Int);

                param_FileId.Value = fileId;

                command.CommandType = CommandType.StoredProcedure;
                command.Connection.Open();
                command.ExecuteNonQuery();
                command.Connection.Close();
            }            
        }
        public void Update()
        {
            using (SqlConnection connection = new SqlConnection(ConnectionString))
            {
                SqlCommand command = new SqlCommand("UploadFiles_Update", connection);
                SqlParameter param_FileId = command.Parameters.Add("@FileId", SqlDbType.Int);
                SqlParameter param_UserId = command.Parameters.Add("@UserId", SqlDbType.Int);
                SqlParameter param_Title = command.Parameters.Add("@Title", SqlDbType.NChar, 255);
                SqlParameter param_FileLink = command.Parameters.Add("@FileLink", SqlDbType.Text);
                SqlParameter param_Enabled = command.Parameters.Add("@Enabled", SqlDbType.Bit);
                SqlParameter param_Count = command.Parameters.Add("@Count", SqlDbType.Int);

                param_FileId.Value = fileId;
                param_UserId.Value = userId;
                param_Title.Value = title;
                param_FileLink.Value = fileLink;
                param_Enabled.Value = enabled;
                param_Count.Value = count;

                command.CommandType = CommandType.StoredProcedure;
                command.Connection.Open();
                command.ExecuteNonQuery();
                command.Connection.Close();
            }         
        }
        public void GetById()
        {
            using (SqlConnection connection = new SqlConnection(ConnectionString))
            {
                SqlCommand command = new SqlCommand("UploadFiles_GetById", connection);
                SqlParameter param_FileId = command.Parameters.Add("@FileId", SqlDbType.Int);
                SqlDataReader reader = null;

                param_FileId.Value = fileId;

                command.CommandType = CommandType.StoredProcedure;
                command.Connection.Open();
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    fileId = (int)reader["FileId"];
                    userId = (int)reader["UserId"];
                    title = reader["Title"].ToString().Trim();
                    fileLink = reader["FileLink"].ToString().Trim();
                    enabled = (bool)reader["Enabled"];
                    pubDate = (DateTime)reader["pubDate"];
                    count = (int)reader["Count"];
                }
                reader.Close();
                command.Connection.Close();
            }        
        }
        public void GetByUser()
        {
            using (SqlConnection connection = new SqlConnection(ConnectionString))
            {
                FileList = new List<UploadFiles>();
                SqlCommand command = new SqlCommand("UploadFiles_GetByUser", connection);
                SqlParameter param_UserId = command.Parameters.Add("@UserId", SqlDbType.Int);
                SqlDataReader reader = null;

                param_UserId.Value = userId;

                command.CommandType = CommandType.StoredProcedure;
                command.Connection.Open();
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    UploadFiles uf = new UploadFiles();
                    uf.fileId = (int)reader["FileId"];
                    uf.GetById();
                    
                    FileList.Add(uf);
                }
                reader.Close();
                command.Connection.Close();
            }         
        }
        public void GetTopTen()
        {
            using (SqlConnection connection = new SqlConnection(ConnectionString))
            {
                FileList = new List<UploadFiles>();
                SqlCommand command = new SqlCommand("UploadFiles_GetTopTen", connection);
                SqlDataReader reader = null;

                command.CommandType = CommandType.StoredProcedure;
                command.Connection.Open();
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    UploadFiles uf = new UploadFiles();
                    uf.fileId = (int)reader["FileId"];
                    uf.GetById();

                    FileList.Add(uf);
                }
                reader.Close();
                command.Connection.Close();
            }
        }
        public void GetAll()
        {
            using (SqlConnection connection = new SqlConnection(ConnectionString))
            {
                FileList = new List<UploadFiles>();
                SqlCommand command = new SqlCommand("UploadFiles_GetAll", connection);
                SqlDataReader reader = null;

                command.CommandType = CommandType.StoredProcedure;
                command.Connection.Open();
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    UploadFiles uf = new UploadFiles();
                    uf.fileId = (int)reader["FileId"];
                    uf.GetById();

                    FileList.Add(uf);
                }
                reader.Close();
                command.Connection.Close();
            }
        }
    }
}